<?php
/**
* This file belongs to a webappkit package (kit)
* @link http://webappkit.net
* @package databeans
* @subpackage http
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
/**
* http response data structure : html/xml/other content, headers etc
* @uses urlBean
*/
class httpResponseBean extends dataBean {
	/**
	* constructor alias
	* @access public
	* /
	function httpResponseBean() {
		waklib::needclass('datatree');
		$this->content=&new datatree();
	}/**/

	/**
	* sends response (headers+content) to client/browser
	* @access public
	* @return bool
	*/
	function sendAll() {
		$this->sendHeaders();
		return $this->sendContent();
	}/**/
	
	/**
	* sends headers to client/browser
	* @access public
	* @return bool
	*/
	function sendHeaders() {
		if ($this->headersSent) return false;
		foreach ($this->headers as $key => $value)
			header("$key: $value");
		$this->headersSent=true;
		return true;
	}/**/
	
	/**
	* sends response content (html page, etc)
	* may use a template if set
	* else joins current data as a string and echoes it
	* @access public
	* @return bool
	*/
	function sendContent() {
		//print_r($this->content);
		// print_r($this->template);
		if (is_object($this->template)) {
			$content=$this->template->render($this->content);
		} else if (is_array($this->content)) {
			// print_r($this->content);
			$content=join('',$this->content);
		} else if (is_string($this->content)) {
			$content=$this->content;
		} else {
			trigger_error("no content ?");
			return false;
		}
		echo $content;
		return true;
	}/**/
	
	/**
	* sets a header to be sent
	* @param string $key
	* @param string $value
	* @access public
	* @return bool
	*/
	function setHeader($key,$value) {
		$this->headers[$key]=&$value;
		return true;
	}/**/
	
	/**
	* prepare a cookie to be sent
	* @param
	* @param
	* @todo
	* /
	function cookie() {
		
	}/**/
	
	/**
	* Inserts some content.
	* @access public
	* @param mixed $content content to insert.
	* @param string $key key or data path.
	* If null, content will be pushed "anonymous" @see dataPath
	* @uses dataPath
	* @return bool
	*/
	function setContent(&$value,$key=null) {
		if (class_exists('datapath')) {
			return dataPath::set($this->content,$value,$key);
		} else if ($key) {
			$this->content[$key]=$value;
		} else {
			$this->content[]=$value;
		}
		// print_r($this->content);
		return true;
	}/**/
	
	/**
	 * gets whole content or a content part
	 * @access public
	 * @param string $key content's part ot get.
	 * If null or unset, returns whole content
	 */
	function getContent($key=null) {
		if (class_exists('datapath')) {
			return dataPath::get($this->content,$key);
		} else if ($key and isset($this->content[$key])) {
			return $this->content[$key];
		} else {
			return $this->content;
		}
	}/**/
	
	/**
	 * Sets template instance for content rendering
	 * The template object have to implement a
	 * <code>string render(array $data)</code> method
	 * @access public
	 * @param object $template
	 * @return bool
	 */
	function setTemplate(&$template) {
		if (!is_object($template)
		or !method_exists($template,'render'))
			return false;
		$this->template=&$template;
		return true;
	}
	
	/**
	* inserts a new content part
	* @param mixed &$content
	* @param string $key optional
	* @return bool
	* /
	function insertContent(&$content, $key=NULL) {
	
	}
	
	
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var array http response headers, key => value
	*/
	var $headers=array(); // http response headers
	/**
	* @var bool have headers been sent yet ?
	*/
	var $headersSent=false;
	/**
	* @var array content storage
	*/
	var $content=array();
	/**
	* @var string mime type
	*/
	var $mime; // mime type
	/**
	* @var array cookies to be sent
	*/
	var $cookies=array();
}
?>